package Ontology;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.net.*;
import java.io.*;
import java.sql.*;

import java.util.*;
import guislicer.Junger;
import guislicer.DocSelector;

import topindex.Index;
import dbindex.DocTable;
import dbindex.Language;

import matching.UnicodeVertexStringer;
import edu.uci.ics.jung.graph.decorators.VertexStringer;
import edu.uci.ics.jung.graph.decorators.NumberEdgeValue;
import edu.uci.ics.jung.visualization.VertexLocationFunction;
import edu.uci.ics.jung.io.PajekNetWriter;

//import junit.framework.*;

//import matching.VertexPairSimilar;
import edu.uci.ics.jung.graph.Graph;
//import edu.uci.ics.jung.graph.Vertex;

public class MainModule implements Runnable
{
	public final static int DISCONNECTED = 1;
	public final static int DISCONNECTING = 2;
	public final static int BEGIN_CONNECT = 3;
	public final static int CONNECTED = 4;
	
	//	GUI components{
	static JFrame jfMainForm = null; 
	static JLabel jlTitle = null;
	static JTextArea jtaHint = null;
	static JButton jbOnto = null;
	static JButton jbStart = null;
	static JButton jbExit = null;
	static JButton jbTest = null;			//	TEMP				
	static JButton jbSlice = null;			//	TEMP
	static JButton jbeDoc = null;			//	TEMP
	static JButton jbServices = null;
	static JComboBox jcbLanguage = null;
	
	//	KSNet components{
	//	static Slice slcSliceByRequest = null;;
	static WDSliceModeller slcSliceByRequest = null;;
	static WebDESOOntology WDO = null;;
	
	//	Common components
	static int iSelectedIndex = 0;		//	selected language in the combobox
	static int iLanguage;				//	a code of the selected language
	
	//	TCP components{
	static ServerSocket ssktKSNet = null;;
	static Socket sktMain = null;;
	static BufferedReader in = null;
	static PrintWriter out = null;
	static int iConnectionStatus = 0;
	
	static StringBuffer toAppend = new StringBuffer("");
	static StringBuffer toSend = new StringBuffer("");

	final static String END_OF_INCOMING_MESSAGE =
		new Character((char)0).toString(); // Indicates the end of a message

	final static MainModule mmObj = new MainModule();
	
	final static String sAnswer = "<html><head></head><body><h1>Summary 2</h1></body></html>";
	
	private static Object [] oSelClasses, oSelAttributes;
	private static Vector vQTop, vQTo, vQFrom;
	
	static private Connection cnDBConnection;
	
	//	final static String SQL_PREPARED_INSERT_STATEMENT_SLICE = "INSERT INTO SliceStorage ( TextRequest, Slice, TemplateID ) VALUES ( ?, ?, ? )";
	final static String SQL_PREPARED_INSERT_STATEMENT_SLICE = "INSERT INTO SliceStorage ( Slice, RequestID, OntoID ) VALUES ( ?, ?, ? )";
	static private PreparedStatement pstInsertSlice;
 

	/**
	 * @param args
	 * Lang=XXX (XXX means 1-3 digits) - pre-defined language
	 */
	public static void main( String[] args ) 
	{
		// TODO Auto-generated method stub
	
		Messages.InitializeCommon();
		Messages.InitializeForAll();
		
		checkInputParameters( args );
		
		initGUI();
		while ( true )
		{
			try
			{
					Thread.sleep( 10 );
			}
			catch ( InterruptedException e ) {};
			
			switch ( iConnectionStatus ) 
			{
				case BEGIN_CONNECT:
				{
					//	iConnectionStatus = 0;
    				try{
    					//	System.out.println( "Starting socket" );
    					ssktKSNet = new ServerSocket( 9999 );
    					
    					//	Give a message
    					iConnectionStatus = 0;
						synchronized ( toAppend ) 
						{
							toAppend.append( "\n\r" +  "HTTP server has been started" );
						}
						SwingUtilities.invokeLater( mmObj );
						
    					sktMain = ssktKSNet.accept();
    					in = new BufferedReader(new InputStreamReader( sktMain.getInputStream()) );
    					out = new PrintWriter( sktMain.getOutputStream(), true);
    					iConnectionStatus = CONNECTED;
    					//	toAppend.append( "\n\r" + "HTTP server has been started" );
        				//	jtaHint.setText( jtaHint.getText() + "\n\r" + "HTTP server has been started" );
    					//	SwingUtilities.invokeLater( mmObj );
    				}
    				catch( IOException ioe)
    				{
    					System.out.println( ioe );
    					toAppend.append( "\n\r" + "Error occures during HTTP server start" );
    					//	jtaHint.setText( jtaHint.getText() + "\n\r" + "HTTP server has not been started" );
    					iConnectionStatus = DISCONNECTED;
    					//	SwingUtilities.invokeLater( mmObj );
    				}
					//	toAppend.append( "\n\r" + "HTTP server has been started" );
    				//	jtaHint.setText( jtaHint.getText() + "\n\r" + "HTTP server has been started" );
					SwingUtilities.invokeLater( mmObj );
					break;
				}	//	BEGIN_CONNECT:
				case CONNECTED:
				{
					//	System.out.println( "I am here..." );
					String s;
					try
					{
						// Receive data
						if (in.ready()) 
						{
							s = in.readLine();
							if ( ( s != null ) &&  ( s.length() != 0 ) ) 
							{
								
			                     // Check if it is the end of a trasmission
			                     if ( s.equals(END_OF_INCOMING_MESSAGE ) ) 
			                     {
			                    	 //	Some actions should be done	*************************************
			                    	 //	sktMain = ssktKSNet.accept();
			                    	 //
			                     }

			                     // Otherwise, receive what text
			                     else 
			                     {
										synchronized ( toAppend ) 
										{
											toAppend.append( "\n\r" +  s);
										}
										SwingUtilities.invokeLater( mmObj );
			                     }
							}	//	something come
						}		//	in.ready()
					}
		            catch ( IOException e ) 
		            {
						synchronized ( toAppend ) 
						{
							toAppend.append( "\n\r" +  "Error during listening port" );
						}
		               cleanUp();
		               SwingUtilities.invokeLater( mmObj );
		            }
					//	SwingUtilities.invokeLater( mmObj );
		            break;
				}	//	CONNECTED:
				default: break;
			}		//	SWITCH
		}			//	LOOP
	}
	
	public void run() 
	{
		switch ( iConnectionStatus ) 
		{
			case DISCONNECTED:
				jbStart.setEnabled( false );
				//	jbStart.setEnabled( true );
				System.out.println( "DISCONNECTED" );
				break;
			case DISCONNECTING:
				jbStart.setEnabled(false);
				System.out.println( "DISCONNECTING" );
				break;
			case CONNECTED:
				jbStart.setEnabled(false);
				System.out.println( "CONNECTED" );
				break;
			case BEGIN_CONNECT:
				jbStart.setEnabled(false);
				System.out.println( "CONNECTING" );
				break;
	      }

	      // Make sure that the button/text field states are consistent
	      // with the internal states
	      jtaHint.append( toAppend.toString() );
	      toAppend.setLength(0);

	      jfMainForm.repaint();
	}
	
	static void initGUI()
	{
		//	prepare screen size
		int iPrefWidth = 1024, iPrefHeight = 768;
		Dimension dScreen = Toolkit.getDefaultToolkit().getScreenSize();
		iPrefWidth = dScreen.width >= iPrefWidth ? iPrefWidth : dScreen.width;
		iPrefHeight = dScreen.height >= iPrefHeight ? iPrefHeight : dScreen.height;
		
		// other dimensions
		int iHintPrefWidth = 1000, iHintPrefHeight = 400;
		
		jfMainForm = new JFrame( "" );
		jfMainForm.setSize( new Dimension( iPrefWidth, iPrefHeight ) );
		//	jfMainForm.setPreferredSize( new Dimension( iPrefWidth, iPrefHeight ) );
		jfMainForm.setLocation( ( dScreen.height - iPrefHeight ) / 2, ( dScreen.width - iPrefWidth ) / 2 );
		jfMainForm.getContentPane().setLayout(new BorderLayout());

		JPanel jpMain = new JPanel();
		jpMain.setLayout( new BoxLayout( jpMain , BoxLayout.Y_AXIS ));	// Up-down		

		jtaHint= new JTextArea( );
		jtaHint.setText( "Hello!\nKSNet middleware has been started." );
		jtaHint.setEditable( false );
		JScrollPane jslHint = new JScrollPane( jtaHint );
		//	jslHint.setSize( new Dimension( iHintPrefWidth, iHintPrefHeight ));
		jslHint.setPreferredSize( new Dimension( iHintPrefWidth, iHintPrefHeight ));
		jtaHint.setBackground( jfMainForm.getBackground());
		//	jslHint.add( jtaHint );

		jbOnto = new JButton( "Ontology" );
		jbStart = new JButton( "Start server" );
		jbExit = new JButton( "Exit" );
		jbTest = new JButton( "Test" );
		jbSlice = new JButton( "Slice" );
		jbeDoc = new JButton( "eDoc" );
		jbServices = new JButton( "Services" );
		
		//	jtxtTitle = new JTextField( "Das ist Title" );
		jlTitle = new JLabel( "The title!" );
		
		jcbLanguage = new JComboBox( );
		
        for (int i = 0; i < Messages.saLanguages.length; i++) {
        	jcbLanguage.addItem( Messages.saLanguages[i] );
        	if ( Messages.iaLanguages[i] == Messages.iCurrentLanguage )iSelectedIndex = i;
        } 
        
        jcbLanguage.setMaximumSize( new Dimension( 100, 22 ));
        jcbLanguage.setSelectedIndex( iSelectedIndex );

        jcbLanguage.addActionListener( new comboBoxAction() );

		JPanel jpTopPanel = new JPanel();
		jpTopPanel.setLayout( new BoxLayout( jpTopPanel, BoxLayout.X_AXIS ));
		
		jpTopPanel.add( jlTitle );
		jpTopPanel.add( Box.createGlue()); //	.createRigidArea( new Dimension( 10, 0 )));
		jpTopPanel.add( jcbLanguage );

        
		JPanel jpButtonPanel = new JPanel();
		
		jpButtonPanel.setLayout( new BoxLayout( jpButtonPanel, BoxLayout.X_AXIS ));
		jpButtonPanel.add( jbOnto );
		jpButtonPanel.add( Box.createRigidArea( new Dimension( 10, 0 )));
		jpButtonPanel.add( jbStart );
		jpButtonPanel.add( Box.createRigidArea( new Dimension( 10, 0 )));
		jpButtonPanel.add( jbTest );
		jpButtonPanel.add( Box.createRigidArea( new Dimension( 10, 0 )));
		jpButtonPanel.add( jbSlice );
		jpButtonPanel.add( Box.createRigidArea( new Dimension( 10, 0 )));
		jpButtonPanel.add( jbeDoc );
		jpButtonPanel.add( Box.createRigidArea( new Dimension( 10, 0 )));
		jpButtonPanel.add( jbServices );
		jpButtonPanel.add( Box.createRigidArea( new Dimension( 10, 0 )));
		jpButtonPanel.add( jbExit );
		
		jbOnto.addActionListener( new PressButton( 1 ) );
		jbStart.addActionListener( new PressButton( 3 ) );
		jbExit.addActionListener( new PressButton( 2 ) );
		jbTest.addActionListener( new PressButton( 4 ) );
		jbSlice.addActionListener( new PressButton( 5 ) );
		jbeDoc.addActionListener( new PressButton( 6 ) );
		jbServices.addActionListener( new PressButton( 7 ) );
		
		jpMain.add( Box.createRigidArea( new Dimension( 0, 20 )));
		jpMain.add( jpTopPanel );
		jpMain.add( Box.createRigidArea( new Dimension( 0, 20 )));
		jpMain.add( jslHint );
		jpMain.add( Box.createRigidArea( new Dimension( 0, 40 )));
		jpMain.add( jpButtonPanel );
		jpMain.add( Box.createRigidArea( new Dimension( 0, 20 )));
		
		jfMainForm.add( jpMain );
		
		jfMainForm.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		//	jfMainForm.setTitle( "KSNet server" );
		setLangDepVar();
		
		jfMainForm.pack();
		jfMainForm.setVisible( true );
	}

	public static void WinMessage( String s, int iMessageFlag )
	{
		JOptionPane.showMessageDialog
		( 
				jfMainForm, 
				s, 
				Messages.sForAll[ Messages.iCurrentLanguage ][23], 
				iMessageFlag 
		);
		return; 
	}

	public static void cleanUp()
	{
        iConnectionStatus = DISCONNECTED;
	    try 
	    {
	    	  if ( ssktKSNet != null ) 
	          {
	    		  ssktKSNet.close();
	    		  ssktKSNet = null;
	          }
	    }
	    catch (IOException ioe ) 
	    { 
	    	System.err.println( ioe );
	    	ssktKSNet = null; 
	    }

	    try 
	    {
	          if ( sktMain != null) {
	        	  sktMain.close();
	        	  sktMain = null;
	          }
	    }
	    catch (IOException ioe ) 
	    { 
	    	System.err.println( ioe );
	    	sktMain = null; 
	    }

	    try 
	    {
	          if ( in != null) {
	             in.close();
	             in = null;
	          }
	    }
	    catch (IOException ioe) 
	    { 
	    	System.err.println( ioe );
	    	in = null; 
	    }

	    if ( out != null) {
	          out.close();
	          out = null;
	    }
	    
		try
		{ 
			if ( cnDBConnection != null ) cnDBConnection.close();
			if ( pstInsertSlice != null ) pstInsertSlice.close();
		}
	    catch ( Exception e ) 
	    {
	        e.printStackTrace( );
	    }
	    return;
	    
	}
	
	private static void checkInputParameters( String[] args )
	{
		String sParValue;
		int iParValue, i, j, k;
			iParValue = 0;
		char[] cTempChar;
			cTempChar = new char[ 1 ];

		iLanguage = 1;	
		if ( args != null ) 
		{
			for ( i = 0; i < args.length; ++i ) 
			{
				sParValue = args[i];
				if ( ( sParValue.length() > 5 ) &&( sParValue.substring( 0, 5 ).equalsIgnoreCase( "Lang=" ) ) & ( iParValue == 0 ) )
					{
						sParValue = sParValue.substring( 5, sParValue.length() ) ; //- 5 );
						iParValue = 0;
						for( j=0; j <= sParValue.length() - 1; j++ )
						{
							sParValue.getChars( j, j + 1, cTempChar, 0 ) ;
							if (  Character.isDigit( cTempChar[0] ) )
								iParValue = iParValue * 10 + Character.getNumericValue( cTempChar[0]);
							else
							{
								iParValue = 0;
								j = sParValue.length() + 1;		// finish a loop
							}
						}
						if ( iParValue > 0 )	//	Parameter is recognized, try to find appropriate language
						{
							for( j=0; j <= Messages.iaLanguages.length - 1; j++ )
							{
								if ( iParValue == Messages.iaLanguages[j] )	//	Finish a loop
									j = Messages.iaLanguages.length + 10;
							}
							if ( j >= Messages.iaLanguages.length + 10 ) //	Language is found!
								iLanguage = iParValue;
							else
								iParValue = 0;
							
						}
				}	// a parameter contains substring "Lang="	
			}		// Loop by parameters			
		}			//	Recognize parameters
		//	System.out.println("Recognized language: " + iLanguage );
		Messages.iCurrentLanguage = iLanguage;
	}
	/**
	 * set language dependent variables
	 *
	 */
	static void setLangDepVar()
	{
		jfMainForm.setTitle( Messages.sForAll[ Messages.iCurrentLanguage ][11] );	//	"KSNet Server"
		jlTitle.setText( Messages.sForAll[ Messages.iCurrentLanguage ][11] );		//	"KSNet Server"
		jbOnto.setText( Messages.sForAll[ Messages.iCurrentLanguage ][32] );			//	Ontology
		jbStart.setText( Messages.sForAll[ Messages.iCurrentLanguage ][21] );		//	Start
		jbTest.setText( Messages.sForAll[ Messages.iCurrentLanguage ][13] );		//	Test
		jbExit.setText( Messages.sForAll[ Messages.iCurrentLanguage ][44] );		//	Exit
		jbServices.setText( Messages.sForAll[ Messages.iCurrentLanguage ][60] );		//	Services
	}

	
	/***************************************
	 * a set of modules devoted to slice preparation
	 *  please, syncronyze with Slicing
	*/
	static boolean buildSlice( int iiOntoID, int[] iiSelClasses, int[] iiSelAttributes, int iiBaseOntoIDTMO )
	{
		//	Object [] oSelClasses, oSelAttributes;
		int [] iSelClasses, iSelAttributes;
		ClassAttr clattrTemp;
		Classes clTempClass;
		Attributes clTempAttr;
		
		int iTempAttrID, iTempClassID;
		boolean bWasFound;
		Object[] oTemp;
		int iCurLang = Messages.iCurrentLanguage;
		
		/*
		if ( WDO == null )
		{
			WinMessage( "Please initialize ontology first", JOptionPane.WARNING_MESSAGE );
			return false;
		}
		*/
		if ( cnDBConnection == null )
		{
			WinMessage( "Please establish connection to database KSNetContext first", JOptionPane.WARNING_MESSAGE );
			return false;
		}
		/*
		if ( isOntologyEmpty() )
		{
			WinMessage( Messages.sForAll[ Messages.iCurrentLanguage ][11], JOptionPane.WARNING_MESSAGE );	// Information about this ontology is absent
			return false;
		}
		*/
		
		slcSliceByRequest = null;
		slcSliceByRequest = new WDSliceModeller( iiOntoID, iiSelClasses, iiSelAttributes, false, iCurLang, iiBaseOntoIDTMO );
		if ( slcSliceByRequest == null )
		{
			WinMessage( "Internal error: can not initialize the slice.", JOptionPane.WARNING_MESSAGE );
			return false;
		}
		
		
		jtaHint.setText( jtaHint.getText() + "\n\r" + "Slice:" );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of classes: " + slcSliceByRequest.vResClasses.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of attributes: " + slcSliceByRequest.vResAttr.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of pairs of classes and attributes: " + slcSliceByRequest.vResClassAttr.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of hierarchical relations: " + slcSliceByRequest.vResHier.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of associative relations: " + slcSliceByRequest.vResAssoc.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of functional constraints: " + slcSliceByRequest.vResFunc.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "Ontology:");
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of classes: " + WDO.vClasses.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of attributes: " + WDO.vAttributes.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of pairs of classes and attributes: " + WDO.vClassAttr.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of hierarchical relations: " + WDO.vWDHier.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of associative relations: " + WDO.vWDAssoc.size() );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "# of functional constraints: " + WDO.vWDFunc.size() );
		
		saveSlice( iiOntoID, iCurLang );
		
		return true;
	}		//	build slice


	private static boolean isOntologyEmpty( ) 
	{
		if ( ( WDO.htClasses.size() == 0 ) && ( WDO.htAttributes.size() == 0 ) ) return true;
		return false;
	}
	
	private static boolean isSelectionCorrect( ) 	//	the function is for debuging purposes only
	{
		for ( int i = 0; i < oSelClasses.length; i++ )
		{
			if ( ! WDO.htClasses.containsKey( oSelClasses[i] ) )
			{
				WinMessage( "Class not found: " + Integer.parseInt( oSelClasses[i].toString() ) , JOptionPane.WARNING_MESSAGE );
				return false;
			}
		};

		for ( int i = 0; i < oSelAttributes.length; i++ )
		{
			if ( ! WDO.htAttributes.containsKey( oSelAttributes[i] ) )
			{
				WinMessage( "Attribut not found: " + Integer.parseInt( oSelAttributes[i].toString() ), JOptionPane.WARNING_MESSAGE );
				return false;
			}
		};

		return true;
	}

	
	static void callEDoc()
	{
		if ( slcSliceByRequest == null )
		{
			WinMessage( "Please prepare slice first", JOptionPane.WARNING_MESSAGE );
			return ;
		}
        
        //	int onto_id = 101;
        int onto_id = 139;
        Language lang = Language.ENGLISH;
        DocSelector ds = new DocSelector();
		List<DocTable> docs = ds.select( cnDBConnection, lang, WDO, onto_id, slcSliceByRequest.vResClassAttr, slcSliceByRequest.vResClasses, slcSliceByRequest.vResAttr, slcSliceByRequest.vResHier, slcSliceByRequest.vResAssoc, slcSliceByRequest.vResFunc,null,null);
		//List<DocTable> docs = ds.select( cnDBConnection, slcSliceByRequest.vResClassAttr, slcSliceByRequest.vResClasses, slcSliceByRequest.vResAttr, slcSliceByRequest.vResHier, slcSliceByRequest.vResAssoc, slcSliceByRequest.vResFunc, null );
		jtaHint.setText( jtaHint.getText() + "\n\r" + "eDOC HAS FINISHED!!!");
		jtaHint.setText( jtaHint.getText() + "\n\r" + "Number of documents: " + docs.size() ); 
		//	docs.

	}
	
	public static void prepareOntology()
	{
 	   jtaHint.setText( jtaHint.getText() + "\n\r" + "Read ontology from repository, please wait..." );
	   //	WDO = new WebDESOOntology( 101 );
	   WDO = new WebDESOOntology( 139, Messages.iCurrentLanguage );
	   jtaHint.setText( jtaHint.getText() + "\n\r" + "Ontology has been read. Waiting for request..." );
	}
	
	public static void startServer()
	{
            if ( cnDBConnection != null )jtaHint.setText( jtaHint.getText() + "\n\r" + "Connection to database is already done!" ); 
            try 
            {
                cnDBConnection = WebDESOOntology.getConnection(Index.WEBDESO_HOST, Index.WEBDESO_USER, Index.WEBDESO_PASS );
                pstInsertSlice = WebDESOOntology.getPreparedStatement( cnDBConnection, SQL_PREPARED_INSERT_STATEMENT_SLICE );
            }
            catch ( Exception eDBConn ) 
            {
                WinMessage( "Can not connect to database.", JOptionPane.WARNING_MESSAGE );
                eDBConn.printStackTrace( );
                return;
            }
            
            //	jtaHint.setText( jtaHint.getText() + "\n\r" + "Starting HTTP server..." );
            //	iConnectionStatus = BEGIN_CONNECT;
            //	mmObj.run();	
	}
	
	private static void saveSlice( int iiOntoID, int iiCurLang )
	{
		//OutputStream pp = new OutputStream();
		ByteArrayOutputStream baosSlice = new ByteArrayOutputStream();
		ObjectOutputStream oosSlice;
		String sRequestText = "aaa";		//	TEMPORARY
		
		try
		{
			oosSlice = new ObjectOutputStream( baosSlice );
		
			oosSlice.writeObject( slcSliceByRequest );	//	
			oosSlice.flush();
	       

			try 
			{
				//	pstInsertSlice.setString( 1, sRequestText );
				pstInsertSlice.setBytes( 1, baosSlice.toByteArray());
				pstInsertSlice.setInt( 2, 0 );	//	request ID
				pstInsertSlice.setInt( 3, iiOntoID );
				pstInsertSlice.executeUpdate();
			} 
			catch( SQLException e ) 
			{
				System.out.println(e);
				WinMessage( "Internal error, can not save slice for request (SQL Query)", JOptionPane.WARNING_MESSAGE );
			}

        	baosSlice.close();
		}
		catch( Exception e ) 
		{
			System.out.println(e);
			WinMessage( "Internal error, can not save slice for request (Byte Stream)", JOptionPane.WARNING_MESSAGE );
        }
		
	}	//	saveSlice()
	
	private static String buildGraph()
	{
	        Junger junger = new Junger();
	        // TestDataWebDeso d = new TestDataWebDeso();
	        Graph g = junger.createGraph( slcSliceByRequest.vResClassAttr, slcSliceByRequest.vResClasses, slcSliceByRequest.vResAttr, slcSliceByRequest.vResHier, slcSliceByRequest.vResAssoc, slcSliceByRequest.vResFunc, null );
	        
	        VertexStringer vs = new UnicodeVertexStringer();
	        NumberEdgeValue nev = null;
	        VertexLocationFunction vld = null;
	        PajekNetWriter writer = new PajekNetWriter();
	        
	        //	String s = writer.saveToString(g, vs, nev, vld);
	        //	System.out.println(s);
	        return writer.saveToString(g, vs, nev, vld);
	}	//	buildGraph
	

}

class comboBoxAction implements java.awt.event.ActionListener
{
	public void actionPerformed( java.awt.event.ActionEvent event )
	{
		if ( MainModule.iSelectedIndex != MainModule.jcbLanguage.getSelectedIndex() )
		{
			MainModule.iSelectedIndex = MainModule.jcbLanguage.getSelectedIndex();
			Messages.iCurrentLanguage = Messages.iaLanguages[ MainModule.iSelectedIndex ];
			MainModule.setLangDepVar();
		}
	}
}

class ActionAdapter implements ActionListener 
{
	public void actionPerformed(ActionEvent e) {}
}


class PressButton extends Component implements ActionListener
{
	private int iSaveTypeOfButton;
	PressButton( int iTypeOfButton )
	{  
		this.iSaveTypeOfButton = iTypeOfButton ;
	}
	
	public void actionPerformed( ActionEvent event )
	{ 
		switch ( this.iSaveTypeOfButton )
		{
			case 1:		// Ontology
			{
				MainModule.prepareOntology();
				break;
			}
			case 3:	//	Start
			{
				MainModule.startServer();
				break;
			}
			case 4:	//	test
			{
				MainModule.WinMessage( "You have pressed button 'Test'", JOptionPane.WARNING_MESSAGE );
				break;
			}
			case 5:	//	Slice
			{
				//	Менять тут!!!
				//	MainModule.buildSlice( 101, new int [] {63219}, new int [] { }, 0 );
				MainModule.buildSlice( 139, new int [] {84197}, new int [] { }, 133 );
				break;
			}
			case 6:	//	Start
			{
				MainModule.callEDoc();
				break;
			}
			case 7:	//	Services
			{
				//	MainModule.WinMessage( "You have pressed button 'Services'", JOptionPane.WARNING_MESSAGE );
				if ( MainModule.slcSliceByRequest == null )MainModule.WinMessage( "Please build a slice first", JOptionPane.WARNING_MESSAGE );
				break;
			}
			case 2:
			{
				MainModule.cleanUp();
		    	System.exit( 0 );
				break;
			}
		}	// end of switch
	}
}	// class PressButton extends Component implements ActionListener

